<?php
namespace Admin\Controller;
use Think\Controller;
class RoleController extends BaseController {
    public function _initialize()
    {
        parent::_initialize();
    }

    public function admin(){
        $list = M('admin')->field(C('DB_PREFIX').'admin.*,'.C('DB_PREFIX').'department.name as d_name')
            ->join("__DEPARTMENT__ on __DEPARTMENT__.id=__ADMIN__.department_id",'left')
            ->order(C('DB_PREFIX').'admin.created_at desc')->select();
        $this->assign('list',$list);
        $this->display();
    }
    public function admin_add(){
        $this->display();
    }
    public function admin_addD($name,$username,$password,$repassword,$department_id){
        if($name==''){
            alert(0,'姓名');
        }
        if($username==''){
            alert(0,'用户名不能为空');
        }
        if($password==''){
            alert(0,'密码不能为空');
        }
        if($password!=$repassword){
            alert(0,'两次密码输入不相同');
        }
        if($department_id==''){
            alert(0,'部门不能为空');
        }
        $count=M('admin')->where("username='".$username."'")->count();
        if($count>0){
            alert(0,'用户名已经存在');
        }
        $data['name']=$name;
        $data['username']=$username;
        $data['salt']=rand(111111,999999);
        $data['password']=md5($password.$data['salt']);
        $data['department_id']=$department_id;
        $data['operator']=session('admin');
        $result=M('admin')->add($data);
        if($result){
            alert(1,'添加成功');
        }else{
            alert(0,'添加失败');
        }
    }
    public function admin_edit($id){
        if($id==''){
            echo "错误";
        }
        $this->data=M('admin')->find($id);
        $this->display();
    }
    public function admin_editD($id,$name,$password,$repassword,$department_id){
        if($name==''){
            alert(0,'姓名');
        }
        if($password!=$repassword){
            alert(0,'两次密码输入不相同');
        }
        if($department_id==''){
            alert(0,'部门不能为空');
        }
        if(M('admin')->where("id='".$id."'")->getField('type')==1 && session('type')==0){
            alert(0,'不能更改超级管理员');
        }
        if($password!=''){
            $data['salt']=rand(111111,999999);
            $data['password']=md5($password.$data['salt']);
        }
        $data['name']=$name;
        $data['department_id']=$department_id;
        $data['operator']=session('admin');
        $result=M('admin')->where("id='".$id."'")->save($data);
        if($result){
            alert(1,'修改成功');
        }else{
            alert(0,'修改失败');
        }
    }
    public function admin_del($id){
        if($id==''){
            alert(0,'操作有误');
        }
        ///////$id=1@2@3
        $group_id=explode('@',$id);
        $id=implode(',',$group_id);
        $u_id=M("admin")->where(array('username'=>session('admin')))->getField('id');
        if(in_array($u_id,$group_id)){
            alert(0,'不允许删除当前账户');
        }
        $admin=M("admin")->where(array('type'=>1))->getField('id',true);
        foreach($group_id as $v){
            if(in_array($v,$admin)){
                alert(0,'不允许删除高级管理员');
            }
        }
        $result=M('admin')->delete($id);
        if($result){
            alert(1,'成功删除了'.$result.'条数据，共'.count($group_id).'条');
        }else{
            alert(0,'删除失败');
        }
    }

    public function department(){
        $f=C('DB_PREFIX');
        $list = M('department')->field($f."department.*,count(".$f."admin.id) as count")
            ->join("__ADMIN__ on __ADMIN__.department_id=__DEPARTMENT__.id",'left')
            ->group("id")->order('created_at desc')->select();
        $this->assign('list',$list);
        $this->display();
    }
    public function department_add(){
        $this->display();
    }
    public function department_addD($name,$group_id){
        $name=trim($name);
        if($name==''){
            alert(0,'部门名称为空');
        }
        if($group_id==''){
            alert(0,'权限不能为空');
        }
        $count=M('department')->where("name='".$name."'")->count();
        if($count>0){
            alert(0,'名称已经存在');
        }
        //////权限检查，是否具备这些权限
        $arr_id=explode(',',$group_id);
        foreach($arr_id as $v){
            if(!is_numeric($v)){
                alert('操作有误');
            }
        }
        $where['id']=array('IN',$arr_id);
        $result=M('root')->where($where)->getField('name',true);
        if(count($result)!=count($arr_id)){
            alert('操作有误');
        }
        $data['name']=$name;
        $data['operator']=session('admin');
        $data['root_group_id']=$group_id;
        $result=M('department')->add($data);
        if($result){
            alert(1,'添加成功');
        }else{
            alert(0,'添加失败');
        }
    }
    public function department_edit($id){
        if($id==''){
            echo "错误";
        }
        $this->data=M('department')->find($id);
        $this->display();
    }
    public function department_editD($id,$group_id,$name){
        $name=trim($name);
        if($group_id==''){
            alert(0,'权限不能为空');
        }
        $count=M('department')->where("name='".$name."' and id <> ".$id)->count();
        if($count>0){
            alert(0,'名称已经存在');
        }
        //////权限检查，是否具备这些权限
        $arr_id=explode(',',$group_id);
        foreach($arr_id as $v){
            if(!is_numeric($v)){
                alert('操作有误');
            }
        }
        $where['id']=array('IN',$arr_id);
        $result=M('root')->where($where)->getField('name',true);
        if(count($result)!=count($arr_id)){
            alert('操作有误');
        }
        $data['operator']=session('admin');
        $data['root_group_id']=$group_id;
        $data['name']=$name;
        $result=M('department')->where(array('id'=>$id))->save($data);
        if($result){
            alert(1,'修改成功');
        }else{
            alert(0,'修改失败');
        }
    }
    public function department_del($id){
        if($id==''){
            alert(0,'操作有误');
        }
        ///////$id=1@2@3
        $group_id=explode('@',$id);
        $id=implode(',',$group_id);
        $result=M('department')->delete($id);
        if($result){
            alert(1,'成功删除了'.$result.'条数据，共'.count($group_id).'条');
        }else{
            alert(0,'删除失败');
        }
    }
    public function getRole(){
        $result=M('root')->select();
        $top=array();
        foreach($result as $k=>$v){
            if($v['is_root']==1){
                $top[]=$v;
                unset($result[$k]);
            }
        }
        foreach($top as $k=>$v){
            foreach($result as $m=>$u){
                if($u['controller']==$v['controller']){
                    $top[$k]['child'][]=$u;
                    unset($result[$m]);
                }
            }
        }
        if($top){
            alert(1,$top);
        }else{
            alert(0,'暂无');
        }
    }
    public function getEditRole($id){
        $result=M('root')->select();
        $root_id=M('department')->where(array('id'=>$id))->getField('root_group_id');
        $arr_root=explode(',',$root_id);
        $top=array();
        foreach($result as $k=>$v){
            if($v['is_root']==1){
                if(in_array($v['id'],$arr_root)){
                    $result[$k]['checked']=1;
                }
                $top[]=$result[$k];
                unset($result[$k]);
            }
        }
        foreach($top as $k=>$v){
            foreach($result as $m=>$u){
                if($u['controller']==$v['controller']){
                    if(in_array($u['id'],$arr_root)){
                        $result[$m]['checked']=1;
                    }
                    $top[$k]['child'][]=$result[$m];
                    unset($result[$m]);
                }
            }
        }
        if($top){
            alert(1,$top);
        }else{
            alert(0,'暂无');
        }
    }
    public function getDepartment(){
        $result=M('department')->select();
        if($result){
            alert(1,$result);
        }else{
            alert(0,'暂无');
        }
    }
}